<!DOCTYPE html>
<!--
Copyright (C) 2016 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-change-actions</title>

<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<script src="../../../scripts/util.js"></script>

<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="gr-change-actions.html">

<test-fixture id="basic">
  <template>
    <gr-change-actions></gr-change-actions>
  </template>
</test-fixture>

<script>
  suite('gr-change-actions tests', function() {
    var element;
    var response = {
      ok: true,
    };
    setup(function(done) {
      stub('gr-rest-api-interface', {
        getChangeRevisionActions: function() {
          return Promise.resolve({
            cherrypick: {
              method: 'POST',
              label: 'Cherry Pick',
              title: 'Cherry pick change to a different branch',
              enabled: true
            },
            rebase: {
              method: 'POST',
              label: 'Rebase',
              title: 'Rebase onto tip of branch or parent change'
            },
            submit: {
              method: 'POST',
              label: 'Submit',
              title: 'Submit patch set 1 into master',
              enabled: true
            }
          });
        },
        send: function(method, url, payload) {
          if (method !== 'POST') { return Promise.reject('bad method'); }

          if (url === '/changes/42/revisions/2/submit') {
            return Promise.resolve({
              ok: true,
              text: function() { return Promise.resolve(')]}\'\n{}'); },
            });
          } else if (url === '/changes/42/revisions/2/rebase') {
            return Promise.resolve({
              ok: true,
              text: function() { return Promise.resolve(')]}\'\n{}'); },
            });
          }

          return Promise.reject('bad url');
        },
      });

      element = fixture('basic');
      element.changeNum = '42';
      element.patchNum = '2';
      element.reload().then(function() { done(); });
    });

    test('submit, rebase, and cherry-pick buttons show', function(done) {
      flush(function() {
        var buttonEls = Polymer.dom(element.root).querySelectorAll('gr-button');
        assert.equal(buttonEls.length, 3);
        assert.isFalse(element.hidden);
        done();
      });
    });

    test('submit change', function(done) {
      flush(function() {
        var submitButton = element.$$('gr-button[data-action-key="submit"]');
        assert.ok(submitButton);
        MockInteractions.tap(submitButton);

        // Upon success it should fire the reload-change event.
        element.addEventListener('reload-change', function(e) {
          done();
        });
      });
    });

    test('submit change with plugin hook', function(done) {
      var canSubmitStub = sinon.stub(element, '_canSubmitChange',
          function() { return false; });
      var fireActionStub = sinon.stub(element, '_fireAction');
      flush(function() {
        var submitButton = element.$$('gr-button[data-action-key="submit"]');
        assert.ok(submitButton);
        MockInteractions.tap(submitButton);
        assert.equal(fireActionStub.callCount, 0);

        canSubmitStub.restore();
        fireActionStub.restore();
        done();
      });
    });

    test('rebase change', function(done) {
      var fireActionStub = sinon.stub(element, '_fireAction');
      flush(function() {
        var rebaseButton = element.$$('gr-button[data-action-key="rebase"]');
        MockInteractions.tap(rebaseButton);
        var rebaseAction = {
          __key: 'rebase',
          __type: 'revision',
          label: 'Rebase',
          method: 'POST',
          title: 'Rebase onto tip of branch or parent change',
        };
        element.$.confirmRebase.base = '1234';
        element._handleRebaseConfirm();
        assert.deepEqual(fireActionStub.lastCall.args,
          ['/rebase', rebaseAction, true, {base: '1234'}]);

        element.$.confirmRebase.base = '';
        element._handleRebaseConfirm();
        assert.deepEqual(fireActionStub.lastCall.args,
          ['/rebase', rebaseAction, true, {}]);

        element.$.confirmRebase.base = 'does not matter';
        element.$.confirmRebase.clearParent = true;
        element._handleRebaseConfirm();
        assert.deepEqual(fireActionStub.lastCall.args,
          ['/rebase', rebaseAction, true, {base: ''}]);

        fireActionStub.restore();
        done();
      });
    });

    test('cherry-pick change', function(done) {
      var fireActionStub = sinon.stub(element, '_fireAction');
      var alertStub = sinon.stub(window, 'alert');

      flush(function() {
        var rebaseButton = element.$$('gr-button[data-action-key="rebase"]');
        MockInteractions.tap(rebaseButton);
        var action = {
          __key: 'cherrypick',
          __type: 'revision',
          enabled: true,
          label: 'Cherry Pick',
          method: 'POST',
          title: 'Cherry pick change to a different branch',
        };

        element._handleCherrypickConfirm();
        assert.equal(fireActionStub.callCount, 0);

        element.$.confirmCherrypick.branch = 'master';
        element._handleCherrypickConfirm();
        assert.equal(fireActionStub.callCount, 0);  // Still needs a message.

        element.$.confirmCherrypick.message = 'foo message';
        element._handleCherrypickConfirm();

        assert.deepEqual(fireActionStub.lastCall.args, [
          '/cherrypick', action, true, {
            destination: 'master',
            message: 'foo message',
          }
        ]);

        fireActionStub.restore();
        alertStub.restore();
        done();
      });
    });


  });
</script>
